#pragma once
#include <iostream>
#include <sys/types.h>
#include <sys/ipc.h>
#include "comm.h"
#include <cstring>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstring>

#define SIZE 4096
int PROJID = 0x11223344;
const std::string PATHNAME = "/home/user_01/test/test_24_5_26/shm";
std::string FILENAME = "fifo";

bool MakeFifo()
{
    int n = mkfifo(FILENAME.c_str(), 0666);
    if(n < 0)
    {
        return false;
    }
    return true;
}

 std::string ToHex(key_t Key)
 {
    char buffer[1024];
    snprintf(buffer,sizeof(buffer),"0x%x",Key);
    return buffer;
 }

key_t GetKey()
{
    key_t Key = ftok(PATHNAME.c_str(), PROJID);
    if (Key < 0)
    {
        std::cerr << "errno " << errno << " ,errstring:" << strerror(errno) << std::endl;
        return 1;
    }
    return Key;
}

int ShmHelp(key_t Key, int flag)
{
    int shmid = shmget(Key, SIZE, flag); // 不存在就创建，存在就错误返回
    if (shmid < 0)
    {
        std::cerr << "errno " << errno << " ,errstring:" << strerror(errno) << std::endl;
        exit(-1);
    }
    return shmid;
}

int CreatShm(key_t Key)
{
    return ShmHelp(Key,IPC_CREAT | IPC_EXCL | 0664);
}

int GetShm(key_t Key)
{
    return ShmHelp(Key,IPC_CREAT);
}



